package panel.viewSpots
{
	import assists.HtmlText;
	import assists.Scale9Grid;
	import assists.TextFieldUtils;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import main.ClientConstants;
	import main.LocalStoreInfo;
	
	import struct.TourElementBase;
	
	public class ViewSpotDetailBtnGroup extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		
		private var _shareBtn:Sprite = new Sprite();
		private var _pathBtn:Sprite = new Sprite();
		private var _collectBtn:Sprite = new Sprite();
		
		private var _scaleBack:Scale9Grid;
		private var _redHeart:Bitmap;
		
		private var _collectBmp:Bitmap;
		private var _storeInfo:LocalStoreInfo = new LocalStoreInfo();
		private var _type:uint;
		
		private var _data:Object;
		private var _onOpenWeibo:Function;
		private var _onAddPath:Function;
		public function ViewSpotDetailBtnGroup(loader:ResourceLoader,stage:Stage)
		{
			_stage = stage;
			_loader = loader;
			
		}
		
		private function init():void
		{
			
			_redHeart = _loader.uiManager.generateBmp(_loader.uiManager.redHeartPng);
			var back:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.btnBackPng);
			_scaleBack = new Scale9Grid(back.bitmapData,10,10);
			_scaleBack.width = _stage.stageWidth;
			
			var shareTxt:TextField = new TextField();
			shareTxt.mouseEnabled = false;
			shareTxt.autoSize = "left";
			TextFieldUtils.setHtmlText(shareTxt,HtmlText.formatSizeColor("分享",0xffffff));
			var shareBmp:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.sharePng);
			shareTxt.x = (shareBmp.width - shareTxt.width) * 0.5;
			shareTxt.y = shareBmp.y + shareBmp.height;
			_shareBtn.addChild(shareBmp);
			_shareBtn.addChild(shareTxt);
			_scaleBack.height = _shareBtn.height * 1.2;
			
			var pathTxt:TextField = new TextField();
			pathTxt.mouseEnabled = false;
			pathTxt.autoSize = "left";
			TextFieldUtils.setHtmlText(pathTxt,HtmlText.formatSizeColor("添加到路线",0xffffff));
			var pathBmp:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.pathPng);
			pathBmp.height = shareBmp.height;
			pathTxt.x = (pathBmp.width - pathTxt.width) * 0.5;
			pathTxt.y = pathBmp.y + pathBmp.height;
			_pathBtn.addChild(pathTxt);
			_pathBtn.addChild(pathBmp);
			
			var collectTxt:TextField = new TextField();
			collectTxt.mouseEnabled = false;
			collectTxt.autoSize = "left";
			TextFieldUtils.setHtmlText(collectTxt,HtmlText.formatSizeColor("收藏",0xffffff));
			_collectBmp = _loader.uiManager.generateBmp(_loader.uiManager.heartPng);
			collectTxt.x = (_collectBmp.width - collectTxt.width) * 0.5;
			collectTxt.y = _collectBmp.y + _collectBmp.height;
			_redHeart.x = _collectBmp.x;
			_redHeart.y = _collectBmp.y;
			if(_storeInfo.readCollectInfo(ClientConstants.CITY_ID,_type,_data.id))//如果未收藏过
			{
				_collectBmp.visible = false;
				_redHeart.visible = true;
			}else
			{
				_collectBmp.visible = true;
				_redHeart.visible = false;
			}
			_collectBtn.addChild(collectTxt);
			_collectBtn.addChild(_collectBmp);
			_collectBtn.addChild(_redHeart);
			
			var line1:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.cutLine,true);
			line1.width = _scaleBack.height * 0.8;
			line1.rotation = 90;
			line1.x = _scaleBack.width / 3;
			line1.y = _scaleBack.height * 0.1;
			
			var line2:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.cutLine,true);
			line2.width = _scaleBack.height * 0.8;
			line2.rotation = 90;
			line2.x = 2 * _scaleBack.width / 3;
			line2.y = _scaleBack.height * 0.1;
			
			
			layout();
			
			this.addChild(_scaleBack);
			this.addChild(_shareBtn);
			this.addChild(_pathBtn);
			this.addChild(_collectBtn);
			this.addChild(line1);
			this.addChild(line2);
			
			_shareBtn.addEventListener(MouseEvent.CLICK,clickShareHandler);
			_pathBtn.addEventListener(MouseEvent.CLICK,clickPahtHandler);
			_collectBtn.addEventListener(MouseEvent.CLICK,clickCollectHandler);
		}
		
		public function onItemInfo(type:uint,info:Object):void
		{
			_type = type;
			_data = info;
			init();
		}
		
		protected function clickCollectHandler(event:MouseEvent):void
		{
			if(!_storeInfo.readCollectInfo(ClientConstants.CITY_ID,_type,_data.id))//如果未收藏过
			{
				_collectBmp.visible = false;
				_redHeart.visible = true;
				_storeInfo.storeCollectInfo(ClientConstants.CITY_ID,_type,_data.id,_data);
			}else
			{
				_collectBmp.visible = true;
				_redHeart.visible = false;
				_storeInfo.removeCollectInfo(ClientConstants.CITY_ID,_type,_data.id);
			}
			trace("collect");
		}
		
		protected function clickPahtHandler(event:MouseEvent):void
		{
			if(_onAddPath is Function){
				_onAddPath(_data);
			}
		}
		
		protected function clickShareHandler(event:MouseEvent):void
		{
			_onOpenWeibo();
		}
		
		public function set onOpenWeibo(value:Function):void
		{
			_onOpenWeibo = value;
		}
		
		public function set onAddPath(value:Function):void
		{
			_onAddPath = value;
		}
		
		private function layout():void
		{
			_shareBtn.y =  (_scaleBack.height - _shareBtn.height) * 0.5; 
			_shareBtn.x = (_scaleBack.width / 3 - _shareBtn.width) * 0.5;
			
			_pathBtn.y = _shareBtn.y;
			_pathBtn.x = (_scaleBack.width / 3 - _shareBtn.width) * 0.5 + _scaleBack.width / 3;
			
			_collectBtn.y = _shareBtn.y;
			_collectBtn.x = (_scaleBack.width / 3 - _collectBtn.width) * 0.5 + 2 * _scaleBack.width / 3 ;
		}
		
		public function clear():void
		{
			_shareBtn.removeEventListener(MouseEvent.CLICK,clickShareHandler);
			_pathBtn.removeEventListener(MouseEvent.CLICK,clickPahtHandler);
			_collectBtn.removeEventListener(MouseEvent.CLICK,clickCollectHandler);
		}
		
	}
}